package com.andreas.simplest;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import static com.andreas.utils.ConnectionUtils.getConnection;

/**
 * 描述：手动确认ack的机制
 */
public class RecerByACK {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = getConnection();
        Channel channel = connection.createChannel();
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String s = new String(body);
                System.out.println("接受 = " + s);
                // 手动确认（收件人信息，是否同时确认多个消息）
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };
        // 4.监听队列，false：手动确认机制，获取到对应的值 接受 = 周乾：Hello,RabbitMQ
        channel.basicConsume("queue1", false, defaultConsumer);
    }
}
